<?php

/**
 * LongTermProgramApplication filter form.
 *
 * @package    sf_sandbox
 * @subpackage filter
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormFilterTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class LongTermProgramApplicationFormFilter extends BaseLongTermProgramApplicationFormFilter {

    public function configure() {
        $this->widgetSchema['course_id'] = new sfWidgetFormDoctrineChoice(array('model' => 'Course', 'add_empty' => true));
        $this->useFields(array('is_approved', 'course_id'));
        $this->validatorSchema->setOption('allow_extra_fields', true);
        $this->validatorSchema->setOption('filter_extra_fields', false);
    }

    public function doBuildQuery(array $values) {
        $q = parent::doBuildQuery($values);
        $user = sfContext::getInstance()->getUser();
        if (isset($values['course_id']) && $values['course_id'] != '') {
            $q->innerJoin('r.TeacherCourse tc');
            $q->addWhere('tc.course_id = ?', $values['course_id']);
            if ($user->hasAttribute('kts_global_program_filter')) {
                $arr = $user->getAttribute('kts_global_program_filter');
                $q->addWhere('tc.academic_year_id = ?', $arr['year']);
            }
        }
        return $q;
    }

}
